package com.health.controller;

import com.health.common.Result;
import com.health.entity.Consultingroom;
import com.health.entity.pojo.PageData;
import com.health.entity.pojo.vo.ConsultingroomVO;
import com.health.entity.pojo.vo.ConsultingroomVO2;
import com.health.service.IConsultingroomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuqingjiang
 * @since 2023-04-17
 */
@RestController
@RequestMapping("/consultingroom")
@Api(tags = "科室管理controller")
public class ConsultingroomController extends BaseController{

    @Autowired
    IConsultingroomService consultingroomService;

    @GetMapping("/listAllConsultingrooms/{pageNum}/{pageSize}")
    @ApiModelProperty("科室分页列表查询")
    public Result listAllConsultingrooms(@PathVariable("pageNum") Integer pageNum,
                                         @PathVariable("pageSize") Integer pageSize){
        PageData consultingroomVOS = consultingroomService.listAllConsultingrooms(pageNum,pageSize);
        List<ConsultingroomVO2> vos = new ArrayList<>();
        List<ConsultingroomVO> list = (List<ConsultingroomVO>) consultingroomVOS.getList();
        for (ConsultingroomVO c : list) {
            ConsultingroomVO2 v = new ConsultingroomVO2();
            v.setId(c.getId());
            v.setName(c.getName());
            String s = "";
            for (ConsultingroomVO consultingroomVO : c.getConsSecond()) {
                s += consultingroomVO.getName()+"\t";
            }
            v.setConsSecond(s);
            vos.add(v);
        }
        consultingroomVOS.setList(vos);
        return this.dataResult(consultingroomVOS);
    }

    @GetMapping("/listSelectConsultingroom/{pageNum}/{pageSize}/{id}")
    @ApiModelProperty("搜索框分页查询")
    public Result listSelectConsultingroom(@PathVariable("pageNum") Integer pageNum,
                                           @PathVariable("pageSize") Integer pageSize,
                                           @PathVariable("id") Long id){
        PageData pageData = consultingroomService.listSelectConsultingroom(pageNum, pageSize, id);
        List<ConsultingroomVO2> vos = new ArrayList<>();
        List<ConsultingroomVO> list = (List<ConsultingroomVO>) pageData.getList();
        for (ConsultingroomVO c : list) {
            ConsultingroomVO2 v = new ConsultingroomVO2();
            v.setId(c.getId());
            v.setName(c.getName());
            String s = "";
            for (ConsultingroomVO consultingroomVO : c.getConsSecond()) {
                s += consultingroomVO.getName()+"\t";
            }
            v.setConsSecond(s);
            vos.add(v);
        }
        pageData.setList(vos);
        return this.dataResult(pageData);
    }

    @PostMapping("/insertConsultingroom")
    @ApiModelProperty("新增二级科室")
    public Result insertConsultingroom(@RequestBody Consultingroom room){

        boolean save = consultingroomService.save(room);
        return this.dataResult(save);
    }

    @DeleteMapping("/deleteConsultingroom/{id}")
    @ApiModelProperty("删除二级科室")
    public Result deleteConsultingroom(@PathVariable("id") Integer id){
        boolean b = consultingroomService.removeById(id);
        return this.dataResult(b);
    }

    @GetMapping("/getConsultingroomByName/{i}")
    @ApiModelProperty("获得二级科室标签")
    public Result getConsultingroomByName(@PathVariable("i") Integer i){
        List<ConsultingroomVO> consultingroomByName = consultingroomService.getConsultingroomByName(i);
        return this.dataResult(consultingroomByName);
    }

}
